<template>
	<view class="num " style="height: 100vh;">
		<block v-if="typeShow == 1 && !successPeo">
			<view class="p-30" style="width: 100%;background-color: #F7FCE6;margin-bottom: 40rpx;">
				<view class="small-size-28" style="color: #8A9668;">
					按国家电子商务法要求，出于上门服务及保障您工钱资金安全的考虑，请进行实名认证。
				</view>
			</view>
			<view class="bgwhite w94 radius20 ">
				<view class="mt-20 flex-between p-30">
					<view class="" style="line-height:2;margin-right: 20rpx;">
						真实姓名
					</view>
					<view class="" style="width: 70%;">
						<u--input placeholder="请输入本人真实姓名"
							prefixIconStyle="font-size: 22px;color: #909399;width:30rpx;height:30rpx;" border="none"
							v-model="username">
						</u--input>
					</view>
				</view>
				<view class="mt-20 flex-between p-30">
					<view class="" style="line-height:2;margin-right: 20rpx;">
						身份证号码
					</view>
					<view class="" style="width: 70%;">
						<u--input placeholder="请输入身份证号码"
							prefixIconStyle="font-size: 22px;color: #909399;width:30rpx;height:30rpx;" border="none"
							v-model="idnum" @blur="checkIdCard" type='text' maxlength="18">
						</u--input>
					</view>
				</view>
			</view>
			<view class="w94 radius20 bgwhite mt20" style="padding: 24rpx;">
				<view class="" style="padding: 24rpx 0;">
					<text style="">上传身份证照片</text>
				</view>
				<view class="">
					<text class="small-size" style="color: red;">当前实名信息必须与您的支付宝、银行卡的实名信息一致，否则无法结算工钱。</text>
				</view>
				<view class="flex-center mt20" @click="upAvater(1)">
					<view class="radius20 flex-center" style="background-color: #F7FCE6; height: 240rpx; width: 70%;">
						<view class="flex-center" v-if="!src1"
							style="width: 100rpx;height: 100rpx;background: #DAF397;border-radius: 50%;">
							<image src="@/static/order/carm.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						</view>
						<image :src="src1" mode="" style="width: 100%;height: 240rpx;" v-else>
						</image>
					</view>
				</view>
				<view class="" style="text-align: center;">
					上传身份证人像页
				</view>
				<view class="flex-center mt20" @click="upAvater(2)">
					<view class="radius20 flex-center" style="background-color: #F7FCE6; height: 240rpx; width: 70%;">
						<view class="flex-center" v-if="!src2"
							style="width: 100rpx;height: 100rpx;background: #DAF397;border-radius: 50%;">
							<image src="@/static/order/carm.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						</view>
						<image :src="src2" mode="" style="width: 100%;height: 240rpx;" v-else>
						</image>
					</view>
				</view>
				<view class="" style="text-align: center;">
					上传身份证国徽页
				</view>
			</view>
			<view class="flex" style="justify-content: center;">
				<view class=""
					style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;margin-top: 40rpx;"
					@click="submit">
					提交
				</view>
			</view>
		</block>
		<block v-if="successPeo">
			<view class="p-30">
				<view class="flex-center">
					<image src="@/static/users/14.png" style="width: 300rpx;height: 300rpx;" mode=""></image>
				</view>
				<view class="" style="text-align: center;">
					恭喜您，已通过实名认证
				</view>
				<view class="m20" style="position: relative;">
					<text>真实姓名：</text><text>{{info.true_name||'王*'}}</text>
					<view class="" style="position: absolute;right: 20rpx;top: 0;">
						👁
					</view>
				</view>
				<view class="m20">
					<text>身份证号：</text><text>{{info.idCard||123456789}}</text>
				</view>
			</view>
		</block>
		<block v-if="typeShow == 2 && !successCom">
			<view class="p-30" style="width: 100%;background-color: #F7FCE6;margin-bottom: 40rpx;">
				<view class="small-size-28" style="color: #8A9668;">
					企业(含个体户)认证通过后，师傅信用分将增加5份，同时能提高中选率。
				</view>
			</view>
			<view class="w94 radius20 bgwhite mt20" style="padding: 24rpx;">
				<view class="" style="padding: 24rpx 0;">
					<text style="">上传营业执照</text>
				</view>
				<view class="">
					<text class="small-size" style="color: #B3B3B3;">上传后将自动识别企业信息，识别后信息无法修改。若识别的结果有误，请重新上传营业执照。</text>
				</view>
				<view class="flex-center mt20" @click="upAvater(3)">
					<view class="radius20 flex-center" style="background-color: #F7FCE6; height: 240rpx; width: 70%;">
						<view class="flex-center" v-if="!src3"
							style="width: 100rpx;height: 100rpx;background: #DAF397;border-radius: 50%;">
							<image src="@/static/order/carm.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						</view>
						<image :src="src3" mode="" style="width: 100%;height: 240rpx;" v-else>
						</image>
					</view>
				</view>
				<view class="" style="text-align: center;">
					营业执照
				</view>
			</view>
			<view class="flex" style="justify-content: center;">
				<view class=""
					style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;margin-top: 40rpx;"
					@click="submit">
					提交
				</view>
			</view>
		</block>
		<block v-if="successCom">
			<view class="" style="position: relative;height: 100vh;">
				<view class="mt20 flex" style="justify-content: center;align-items: center;">
					<image src="@/static/order/success.png" mode="" style="width: 120rpx;height: 120rpx;"></image>
				</view>
				<view class="mt20" style="text-align: center;font-size: 40rpx;">
					营业执照已上传
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import {
		uploadFile,
		uploadFiles,
		profile
	} from '@/common/teacherReq';
	export default {
		data() {
			return {
				typeShow: 1,
				successPeo: false,
				successCom: false,
				username: '',
				idnum: '',
				src1: '', // 身份证正面
				src2: '', // 身份证反面
				src3: '', // 营业执照
			};
		},
		computed: {
			info() {
				return uni.getStorageSync('info')
			}
		},
		onLoad(opt) {
			this.typeShow = Number(opt.type)
			if (this.typeShow == 1) {
				uni.setNavigationBarTitle({
					title: '实名认证'
				})
				if (this.info.idCard) this.successPeo = true
			} else {
				uni.setNavigationBarTitle({
					title: '企业认证'
				})
				if (this.info.qiye_photo) this.successCom = true
			}
		},
		methods: {
			submit() {
				const data = this.typeShow === 1 ? {
					idCard: this.idnum,
					true_name: this.username,
					shenfen_zheng: this.src1,
					shenfen_fan: this.src2,
				} : {
					qiye_photo: this.src3
				};

				profile(data).then(res => {
					this.typeShow === 1 ? this.successPeo = true : this.successCom = true;
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index',
						});
					}, 1500);
				});
			},
			successComSubmit() {
				this.successCom = true;
			},
			upAvater(value) {
				uni.chooseImage({
					count: 1, // 只选择1张图片
					sizeType: ['original', 'compressed'], // 支持原图和压缩图
					sourceType: ['album'], // 从相册选择
					success: async (res) => {
						const tempFilePath = res.tempFilePaths[0];
						// 先设置临时路径
						this.setSrcByValue(value, tempFilePath);

						try {
							// 上传文件
							const resp = await uploadFile({
								filePath: tempFilePath
							});
							// 设置服务器返回的url
							const fullUrl = JSON.parse(resp[1].data).data.fullurl;
							this.setSrcByValue(value, fullUrl);
						} catch (err) {
							uni.showToast({
								icon: 'error',
								title: '上传失败:' + err
							});
						}
					}
				});
			},

			// 根据value设置对应的src
			setSrcByValue(value, url) {
				switch (value) {
					case 1:
						this.src1 = url;
						break;
					case 2:
						this.src2 = url;
						break;
					case 3:
						this.src3 = url;
						break;
				}
			},
			// 身份证号码校验规则
			checkIdCard(idCard) {
				// 15位和18位身份证号码的正则表达式
				const regIdCard = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

				if (!regIdCard.test(idCard)) {
					uni.showToast({
						title: '身份证号码格式不正确',
						icon: 'none'
					});
					return false;
				}

				// 如果是15位身份证则转为18位
				if (idCard.length == 15) {
					idCard = this.idCardUpdate(idCard);
				}

				// 验证生日是否正确
				const birthday = idCard.substr(6, 4) + '/' + idCard.substr(10, 2) + '/' + idCard.substr(12, 2);
				if (new Date(birthday) > new Date()) {
					uni.showToast({
						title: '身份证号码生日不正确',
						icon: 'none'
					});
					return false;
				}

				// 验证校验位
				if (idCard.length == 18) {
					const idCardWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
					const idCardY = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
					let idCardWiSum = 0;

					for (let i = 0; i < 17; i++) {
						idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
					}

					const idCardMod = idCardWiSum % 11;
					const idCardLast = idCard.substring(17);

					if (idCardY[idCardMod].toLowerCase() != idCardLast.toLowerCase()) {
						uni.showToast({
							title: '身份证号码校验位不正确',
							icon: 'none'
						});
						return false;
					}
				}

				return true;
			},

			// 15位转18位身份证号
			idCardUpdate(idCard) {
				let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
				let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
				let cardTemp = 0;
				let i;

				idCard = idCard.substr(0, 6) + '19' + idCard.substr(6, idCard.length - 6);
				for (i = 0; i < 17; i++) {
					cardTemp += idCard.substr(i, 1) * arrInt[i];
				}

				idCard += arrCh[cardTemp % 11];
				return idCard;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nameauth {}
</style>